package com.walter.security;

import cn.hutool.json.JSONUtil;
import com.walter.AuthUser;
import com.walter.LoginInfoHolder;
import com.walter.entity.User;
import com.walter.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * @author ：ch
 * @date ：Created in 2019/5/21
 * @description：
 * @modified By：
 * @version: 1.0
 */
@Service
public class AuthUserService implements UserDetailsService {
    @Autowired
    private UserMapper userMapper;

    // 加载用户信息
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userMapper.selectOne(new User().setUserName(username));
        AuthUser authUser = new AuthUser(JSONUtil.parseObj(user));
        LoginInfoHolder.setSalt(user.getSalt());
        return (UserDetails) authUser;
    }
}
